﻿using Application.CodeRuleService;
using Domain.Entity;
using Domain.Entity.sys;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WMSManage.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]//需要鉴权
    public class CodeRuleController : ControllerBase
    {
        private readonly ICodeRuleService codeRuleService;

        public CodeRuleController(ICodeRuleService codeRuleService)
        {
            this.codeRuleService = codeRuleService;
        }


        /// <summary>
        /// 分页查询编号规则信息
        /// </summary>
        /// <param name="name">目标表单</param>
        /// <param name="prefix">前缀</param>
        /// <param name="timerule">时间规则</param>
        /// <param name="starttime">开始时间</param>
        /// <param name="endtime">结束时间</param>
        /// <param name="CurrentPage">当前页</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FenYe<CodeRule>> GetCodeRule(string? name, string? prefix, string? timerule, string? starttime, string? endtime,int CurrentPage)
        {
            FenYe<CodeRule> fenYe = new FenYe<CodeRule>()
            {
                CurrentPage=CurrentPage,
            };
            return await codeRuleService.GetCodeRule(fenYe,name, prefix, timerule, starttime, endtime);
        }


        /// <summary>
        /// 添加编号规则信息
        /// </summary>
        /// <param name="addCodeRuleDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddCodeRule(AddCodeRuleDto addCodeRuleDto)
        {
            return await codeRuleService.AddCodeRule(addCodeRuleDto);
        }


        /// <summary>
        /// 删除编号规则信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> DeleteCodeRule(int id)
        {
            return await codeRuleService.DeleteCodeRule(id);
        }


        /// <summary>
        /// 根据id显示编号规则信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<CodeRule> GetCodeRuleByid(int id)
        {
            return await codeRuleService.GetCodeRuleByid(id);
        }


        /// <summary>
        /// 修改编号规则信息
        /// </summary>
        /// <param name="updateCodeRuleDto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateCodeRule(UpdateCodeRuleDto updateCodeRuleDto)
        {
            return await codeRuleService.UpdateCodeRule(updateCodeRuleDto);
        }


        /// <summary>
        /// 获取生成的编号
        /// </summary>
        /// <param name="id">编号规则id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<string> GeneratedCode(int id)
        {
            return await codeRuleService.GeneratedCode(id);
        }
    }
}
